/*     */ package com.zimbra.cs.iochannel;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import java.io.IOException;
/*     */ import java.nio.ByteBuffer;
/*     */ import java.nio.CharBuffer;
/*     */ import java.nio.charset.Charset;
/*     */ import java.nio.charset.CharsetDecoder;
/*     */ import java.nio.charset.CharsetEncoder;
/*     */ import java.util.HashMap;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class Message
/*     */ {
/*  81 */   private static final HashMap<String, Message> messages = new HashMap();
/*     */   private static final int padding = 256;
/*     */   
/*  84 */   static { registerMessage(new CrossServerNotification());
/*  85 */     registerMessage(new MailboxNotification());
/*     */   }
/*     */   
/*     */   public static void registerMessage(Message m) {
/*  89 */     synchronized (messages) {
/*  90 */       messages.put(m.getAppId(), m);
/*     */     }
/*     */   }
/*     */   
/*     */   public static Message create(ByteBuffer buffer) throws IOException {
/*  95 */     int len = buffer.getInt();
/*  96 */     byte[] appId = new byte[len];
/*  97 */     buffer.get(appId);
/*  98 */     String appIdStr = new String(appId, "UTF-8");
/*  99 */     Message m = (Message)messages.get(appIdStr);
/* 100 */     if (m != null) {
/* 101 */       return m.construct(buffer);
/*     */     }
/* 103 */     throw MessageChannelException.NoSuchMessage(appIdStr);
/*     */   }
/*     */   
/*     */   private int messageSize() throws IOException
/*     */   {
/* 108 */     return size() + getAppId().length() * 2 + 4 + 256;
/*     */   }
/*     */   
/*     */   public ByteBuffer serialize() throws IOException {
/* 112 */     ByteBuffer buffer = ByteBuffer.allocate(messageSize());
/* 113 */     writeString(buffer, getAppId());
/* 114 */     serialize(buffer);
/* 115 */     buffer.flip();
/* 116 */     return buffer;
/*     */   }
/*     */   
/*     */   protected Message() {
/* 120 */     this(null);
/*     */   }
/*     */   
/*     */   protected Message(Charset c) {
/* 124 */     this.charset = (c != null ? c : Charset.forName("UTF-8"));
/*     */   }
/*     */   
/*     */   protected ByteBuffer encode(String str) throws IOException {
/* 128 */     return this.charset.newEncoder().encode(CharBuffer.wrap(str));
/*     */   }
/*     */   
/*     */   protected String decode(ByteBuffer buffer) throws IOException {
/* 132 */     return this.charset.newDecoder().decode(buffer).toString();
/*     */   }
/*     */   
/*     */   protected void writeString(ByteBuffer buffer, String str) throws IOException {
/* 136 */     ByteBuffer encodedBuffer = encode(str);
/* 137 */     buffer.putInt(encodedBuffer.limit());
/* 138 */     buffer.put(encodedBuffer);
/*     */   }
/*     */   
/*     */   protected String readString(ByteBuffer buffer) throws IOException {
/* 142 */     int len = buffer.getInt();
/* 143 */     ByteBuffer sub = buffer.slice();
/* 144 */     sub.limit(len);
/* 145 */     buffer.position(buffer.position() + len);
/* 146 */     return decode(sub);
/*     */   }
/*     */   
/*     */ 
/* 150 */   protected static Log log = LogFactory.getLog("iochannel");
/*     */   protected final Charset charset;
/*     */   
/*     */   public abstract String getAppId();
/*     */   
/*     */   public abstract String getRecipientAccountId();
/*     */   
/*     */   protected abstract int size();
/*     */   
/*     */   protected abstract void serialize(ByteBuffer paramByteBuffer)
/*     */     throws IOException;
/*     */   
/*     */   protected abstract Message construct(ByteBuffer paramByteBuffer)
/*     */     throws IOException;
/*     */   
/*     */   public abstract MessageHandler getHandler();
/*     */   
/*     */   public static abstract interface MessageHandler
/*     */   {
/*     */     public abstract void handle(Message paramMessage, String paramString);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/iochannel/Message.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */